<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'bus:extd:yfYlfw:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
        <!-- <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button> -->

        <a-button type="default" @click="handleExport1()" v-auth="'bus:extd:yfYlfw:export'">
          <Icon icon="ant-design:download-outlined" /> {{ t('对账单导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.ddate }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />

    <!-- 对账单 -->
    <BasicModal v-bind="$attrs" @register="registerModal" title="对账单导出" @ok="handleModal">
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBusExtdYfYlfwList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { yfYlfwDelete, yfYlfwListData } from '/@/api/bus/extd/yfYlfw';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl2, downloadByUrl } from '/@/utils/file/download';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { m8ViewHospTreeData } from '/@/api/bas/hosp/m8ViewHosp';

  const { t } = useI18n('bus.extd.yfYlfw');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('医疗废物管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('日期起'),
        field: 'ddate_gte',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('日期止'),
        field: 'ddate_lte',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('线路'),
        field: 'croute',
        component: 'Input',
      },
      {
        label: t('医院'),
        field: 'hospName',
        component: 'Input',
      },
      {
        label: t('区域'),
        field: 'area',
        component: 'Input',
      },
      {
        label: t('线路顺序'),
        field: 'routeSort',
        component: 'Input',
      },
      {
        label: t('收运车辆'),
        field: 'carNo',
        component: 'Input',
      },
      {
        label: t('驾驶员'),
        field: 'driver',
        component: 'Input',
      },
      {
        label: t('押运员'),
        field: 'driver2',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('日期'),
      dataIndex: 'ddate',
      key: 'a.日期',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('线路'),
      dataIndex: 'croute',
      key: 'a.线路',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('医院'),
      dataIndex: 'hospName',
      key: 'a.医院',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('感染性'),
      dataIndex: 'weightGgr',
      key: 'a.感染性',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('损伤性'),
      dataIndex: 'weightPs',
      key: 'a.损伤性',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('总量'),
      dataIndex: 'weightSum',
      key: 'a.总量',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('区域'),
      dataIndex: 'area',
      key: 'a.区域',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('感染性箱数'),
      dataIndex: 'qtyGr',
      key: 'a.感染性箱数',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('损伤性箱数'),
      dataIndex: 'qtyPs',
      key: 'a.损伤性箱数',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('总箱数'),
      dataIndex: 'qtySum',
      key: 'a.总箱数',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('线路顺序'),
      dataIndex: 'routeSort',
      key: 'a.线路顺序',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('医疗机构性质'),
      dataIndex: 'ctype',
      key: 'a.医疗机构性质',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('收运车辆'),
      dataIndex: 'carNo',
      key: 'a.收运车辆',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('驾驶员'),
      dataIndex: 'driver',
      key: 'a.驾驶员',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('押运员'),
      dataIndex: 'driver2',
      key: 'a.押运员',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('收运时间'),
      dataIndex: 'shDate',
      key: 'a.收运时间',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('接收时间'),
      dataIndex: 'jsDate',
      key: 'a.接收时间',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('处置时间'),
      dataIndex: 'dealDate',
      key: 'a.处置时间',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('联单编号'),
      dataIndex: 'extDjno',
      key: 'a.联单编号',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存档'),
      dataIndex: 'bsp',
      key: 'a.存档',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑医疗废物'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bus:extd:yfYlfw:edit',
      },
    ],
  };

  const [registerModal, { openModal, closeModal }] = useModal();
  const [registerForm, { resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: [
      {
        label: t('导出日期'),
        field: 'date',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM',
          valueFormat: 'YYYY-MM',
          showTime: false,
        },
        required: true,
      },
      {
        label: t('归属医院'),
        field: 'hospCode',
        fieldLabel: 'hospName',
        component: 'TreeSelect',
        componentProps: {
          api: m8ViewHospTreeData,
          params: { isShowCode: false },
          // canSelectParent: false,
          allowClear: true,
        },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: yfYlfwListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    const params = {};
    const data = getForm().getFieldsValue();
    downloadByUrl2({
      url: ctxAdminPath + '/bus/extd/yfYlfw/exportData',
      params,
      data,
      // target: '_self',
    });
  }

  async function handleExport1() {
    await openModal(true, {});
    resetFields();
  }

  async function handleModal() {
    let data = await validate();
    const { ctxPath } = useGlobSetting();
    downloadByUrl({
          url: ctxPath + `/ureport/pdf?_u=file:医院对账单.ureport.xml&date=${data.date}&hospName=${data.hospName}`,
          target: '_self',
          // fileName: `${record.value.projectno}-${record.value.projectname}.pdf`,
        });
    closeModal();
  }

  async function handleDelete(record: Recordable) {
    const res = await yfYlfwDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>